import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import dotenv from 'dotenv';

// 加载.env文件
dotenv.config();

export default defineConfig({
  plugins: [
    react({
      jsxRuntime: 'automatic',
      fastRefresh: true,
      babel: {
        babelrc: false,
        configFile: false,
      }
    }),
    {
      name: 'html-transform',
      transformIndexHtml(html) {
        return html.replace(
          '%AMAP_SECURITY_JS_CODE%',
          process.env.AMAP_SECURITY_JS_CODE || ''
        );
      },
    },
  ],
  server: {
    port: 5173,
    host: true,
    proxy: {
      '/api': {
        target: 'http://localhost:9999',
        changeOrigin: true,
        secure: false
      },
      '/socket.io': {
        target: 'http://localhost:9999',
        changeOrigin: true,
        ws: true
      }
    }
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
    rollupOptions: {
      output: {
        manualChunks: {
          'react-vendor': ['react', 'react-dom'],
        }
      }
    }
  },
  define: {
    global: 'globalThis',
    'process.env.AMAP_KEY': JSON.stringify(process.env.AMAP_KEY),
    'process.env.AMAP_SECURITY_JS_CODE': JSON.stringify(process.env.AMAP_SECURITY_JS_CODE)
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      'crypto': path.resolve(__dirname, 'node_modules/crypto-browserify')
    }
  },
  optimizeDeps: {
    include: ['react', 'react-dom'],
    esbuildOptions: {
      loader: {
        '.js': 'jsx',
        '.ts': 'tsx',
        '.jsx': 'jsx',
        '.tsx': 'tsx'
      },
      jsx: 'automatic',
      jsxImportSource: 'react'
    }
  }
}); 